package com.heo.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.net.ssl.SSLContext;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpHelper {
	// Increase max total connection to 200
	private static PoolingHttpClientConnectionManager cm = null;
	private static Logger log = Logger.getLogger(HttpHelper.class);

	/**
	 * 向服务端请求超时时间设置(单位:毫秒)
	 */
	private static int SERVER_REQUEST_TIME_OUT = 5 * 1000;

	/**
	 * 服务端响应超时时间设置(单位:毫秒)
	 */
	private static int SERVER_RESPONSE_TIME_OUT = 10 * 1000;

	private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SERVER_REQUEST_TIME_OUT)
			.setConnectTimeout(SERVER_RESPONSE_TIME_OUT).build();

	static {
		cm = new PoolingHttpClientConnectionManager();
		cm.setMaxTotal(5000);// 设置http连接池大小
		cm.setDefaultMaxPerRoute(500);// 单路由最大大小,设置每个路由默认连接数
	}

	private HttpHelper() {
		// Increase default max connection per route to 20
		// cm.setMaxTotal(SysConfig.getInstance().getHttpMaxTotal());
		// Increase max connections for localhost:80 to 50
		// cm.setDefaultMaxPerRoute(SysConfig.getInstance().getHttpDefaultMaxPerRoute());
		// HttpHost localhost = new HttpHost("locahost", 80);
		// cm.setMaxPerRoute(new HttpRoute(localhost), 50);
	}

	public static String sendHttpGet(String url) {
		HttpGet httpGet = null;
		CloseableHttpResponse response = null;
		CloseableHttpClient httpclient = null;
		try {
			// 如果循环完毕还没有找到，那么就发送请求去管理系统中找
			httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm)
					.setConnectionManagerShared(true).build();
			httpGet = new HttpGet(url);
			httpGet.addHeader("Connection", "close");
			response = httpclient.execute(httpGet);
			String content = EntityUtils.toString(response.getEntity());
			EntityUtils.consume(response.getEntity());// 关闭数据流
			if (response.getStatusLine().getStatusCode() == 200) {
				return content;
			} else {
				return null;
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return null;
		} finally {
			if (httpGet != null) {
				try {
					httpGet.releaseConnection();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (response != null) {
				try {
					response.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
		}
	}

	public static String sendHttpsGet(String url) throws Exception {
		CloseableHttpClient httpclient = null;
		CloseableHttpResponse response = null;
		HttpGet httpget = null;
		try {
			// Trust own CA and all self-signed certs
			SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
			// Allow TLSv1 protocol only
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
					null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
			httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm)
					.setConnectionManagerShared(true).setSSLSocketFactory(sslsf).build();
			// HttpGet httpget = new HttpGet(url);
			httpget = new HttpGet(url);
			httpget.addHeader("Connection", "close");
			response = httpclient.execute(httpget);
			log.debug("Executing request " + httpget.getRequestLine());
			return EntityUtils.toString(response.getEntity(), "UTF-8");
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		} finally {
			if (httpget != null) {
				try {
					httpget.releaseConnection();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (response != null) {
				try {
					response.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
		}
		return null;
	}

	public static String sendHttpPosts(String url, String content) throws Exception {
		CloseableHttpClient httpclient = null;
		CloseableHttpResponse response = null;
		HttpPost httppost = null;
		try {
			SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
					null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
			httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm)
					.setConnectionManagerShared(true).setSSLSocketFactory(sslsf).build();
			httppost = new HttpPost(url);
			httppost.addHeader("Connection", "close");
			httppost.setEntity(new StringEntity(content, "UTF-8"));
			log.debug("执行post请求： " + httppost.getRequestLine());
			response = httpclient.execute(httppost);
			String backStr = EntityUtils.toString(response.getEntity(), "UTF-8");
			EntityUtils.consume(response.getEntity());// 关闭数据流
			log.debug("返回结果：" + backStr);
			return backStr;
		} finally {
			if (httppost != null) {
				try {
					httppost.releaseConnection();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (response != null) {
				try {
					response.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
		}
	}
	
	public static String sendHttpPost(String url, String content) throws Exception {
		CloseableHttpClient httpclient = null;
		CloseableHttpResponse response = null;
		HttpPost httppost = null;
		try {
			httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm)
					.setConnectionManagerShared(true).build();
			httppost = new HttpPost(url);
			httppost.addHeader("Connection", "close");
			httppost.setEntity(new StringEntity(content, "UTF-8"));
			log.debug("执行post请求： " + httppost.getRequestLine());
			response = httpclient.execute(httppost);
			String backStr = EntityUtils.toString(response.getEntity(), "UTF-8");
			EntityUtils.consume(response.getEntity());// 关闭数据流
			log.debug("返回结果：" + backStr);
			return backStr;
		} finally {
			if (httppost != null) {
				try {
					httppost.releaseConnection();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (response != null) {
				try {
					response.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
		}
	}
	
	public static String sendHttpPost(String url, Map<String, String> map) throws Exception {
		CloseableHttpClient httpclient = null;
		CloseableHttpResponse response = null;
		HttpPost httppost = null;
		try {
			httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm)
					.setConnectionManagerShared(true).build();
			httppost = new HttpPost(url);
			httppost.addHeader("Connection", "close");
			//装填参数
	        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	        if(map!=null){
	            for (Entry<String, String> entry : map.entrySet()) {
	                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
	            }
	        }
	        //设置参数到请求对象中
	        httppost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			log.debug("执行post请求： " + httppost.getRequestLine());
			response = httpclient.execute(httppost);
			String backStr = EntityUtils.toString(response.getEntity(), "UTF-8");
			EntityUtils.consume(response.getEntity());// 关闭数据流
			log.debug("返回结果：" + backStr);
			return backStr;
		} finally {
			if (httppost != null) {
				try {
					httppost.releaseConnection();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (response != null) {
				try {
					response.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
			if (httpclient != null) {
				try {
					httpclient.close();
				} catch (Exception e2) {
					log.error(e2.getMessage(), e2);
				}
			}
		}
	}

	public static void main(String[] args) {

	}

}
